/*
 * @Author: your name
 * @Date: 2021-05-20 16:14:57
 * @LastEditTime: 2021-05-20 16:17:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \three_demo\src\pages\3dScreen\components\center\model1.js
 */
let fnGear = ()=>{
    // let position = intersect.position;
        
        // let x = position.x;
        // let y = position.y;
        // let z = position.z;

        // const material = new THREE.LineBasicMaterial({
        //     color: "#f56c09",
        //     linewidth: 1000,
        //     linecap: 'butt', //ignored by WebGLRenderer
        //     linejoin:  'bevel' //ignored by WebGLRenderer
        // });
        
        // const points = [];
        // const srcP = new THREE.Vector3(x,y,z);
        // points.push(srcP);
        // points.push( new THREE.Vector3( x, y, z-250 ) );
        // points.push( new THREE.Vector3( x+150, y, z-250 ) );
        
        // const geometry = new THREE.BufferGeometry().setFromPoints( points );
        
        // const line = new THREE.Line( geometry, material );
        // scene.add( line );
    fnInitCamera();
    fnInitScene();
    fnInitLight();

    var group = new THREE.Group();
    let i=1;
    // fnInitTest1();
    // drawBSP();
    // test1();
    // test2();
    fnRenderer();
    fnControls();

    
    function fnInitTest1(){
        var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
        var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );

        var cubeA = new THREE.Mesh( geometry, material );
        cubeA.position.set( 100, 100, 0 );

        var cubeB = new THREE.Mesh( geometry, material );
        cubeB.position.set( -100, -100, 0 );

        //create a group and add the two cubes
        //These cubes can now be rotated / scaled etc as a group
        
        group.add( cubeA );
        group.add( cubeB );

        // group.rotation.x = - Math.PI / 2;
        // group.position.y = 11;
        // group.rotation.x = Math.PI/3;
        scene.add( group );
    }

    function drawBSP() {
        var material = new THREE.MeshPhongMaterial({color: 0x2C85E1, shininess: 60, specular: 0x2C85E1})

        var cylinGeom1 = new THREE.CylinderGeometry(40, 40, 10, 50)
        var cylinMesh1 = new THREE.Mesh(cylinGeom1, material);
        cylinMesh1.position.y = 5;

        var cylinGeom2 = new THREE.CylinderGeometry(28, 28, 14, 50)
        var cylinMesh2 = new THREE.Mesh(cylinGeom2, material);
        cylinMesh2.position.y = 7;

        var cylinGeom3 = new THREE.CylinderGeometry(20, 20, 22, 50)
        var cylinMesh3 = new THREE.Mesh(cylinGeom3, material);
        cylinMesh3.position.y = 11;

        var cylinGeom4 = new THREE.CylinderGeometry(10, 10, 24, 50)
        var cylinMesh4 = new THREE.Mesh(cylinGeom4, material);
        cylinMesh4.position.y = 12;

        var pointsArr = [[5, -1, 33], [5, 11, 33], [-5, -1, 33], [-5, 11, 33], [20, -1, 66], [20, 11, 66], [-20, -1, 66], [-20, 11, 66]];
        var points = pointsArr.map(d => new THREE.Vector3(d[0],d[1],d[2]));
        var tixing = new ConvexGeometry(points);
        var tixingMesh = new THREE.Mesh(tixing, material);

        var meshArray = [];
        for(var i=0; i<8; i++) {
            tixingMesh.rotation.y = Math.PI / 4 * i;
            meshArray[i] = tixingMesh.clone()
        }

        var cylinBSP1 = new ThreeBSP(cylinMesh1);
        var cylinBSP2 = new ThreeBSP(cylinMesh2);
        var cylinBSP3 = new ThreeBSP(cylinMesh3);
        var cylinBSP4 = new ThreeBSP(cylinMesh4);

        var resultBSP = cylinBSP1.union(cylinBSP2).union(cylinBSP3).subtract(cylinBSP4);
        // for(var i=0; i<8; i++) {
        //     let mes = meshArray[i];
        //     console.log(mes,'........');
        //     let res = new ThreeBSP(mes);
        //     console.log(res);
        //     resultBSP = resultBSP.subtract(res);
        // }
        let result = resultBSP.toMesh();
        result.material = new THREE.MeshPhongMaterial({color: 0x2C85E1, shininess: 60, specular: 0x2C85E1});
        result.position.y = 20;
        scene.add(result);
    }

    function test1(){
        // var length = 12, width = 8;
        // var shape = new THREE.Shape();
        // shape.moveTo( 0,0 );
        // shape.lineTo( 0, width );
        // shape.lineTo( length, width );
        // shape.lineTo( length, 0 );
        // shape.lineTo( 0, 0 );

        // var extrudeSettings = {
        //     steps: 2,
        //     depth: 16,
        //     bevelEnabled: true,
        //     bevelThickness: 1,
        //     bevelSize: 1,
        //     bevelSegments: 1
        // };

        // var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
        // var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
        // var mesh = new THREE.Mesh( geometry, material ) ;
        // scene.add( mesh );

        // var geometry = new THREE.RingGeometry( 1, 5, 32 );
        // var material = new THREE.MeshBasicMaterial( { color: 0xffff00, side: THREE.DoubleSide } );
        // var mesh = new THREE.Mesh( geometry, material );
        // scene.add( mesh );
        
    }

    function test2(){
        var curve = new THREE.CubicBezierCurve3(
            new THREE.Vector3( -10, 0, 0 ),
            new THREE.Vector3( -5, 15, 0 ),
            new THREE.Vector3( 20, 15, 0 ),
            new THREE.Vector3( 10, 0, 0 )
        );
        
        var points = curve.getPoints( 50 );
        var geometry = new THREE.BufferGeometry().setFromPoints( points );
        
        var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
        
        // Create the final object to add to the scene
        var curveObject = new THREE.Line( geometry, material );
        scene.add( curveObject );
    }

    animate();
    function fbx1(){
        loader.load( './models/fbx/SambaDancing.fbx', function ( object ) {

        mixer = new THREE.AnimationMixer( object );

        const action = mixer.clipAction( object.animations[ 0 ] );
        action.play();

        object.traverse( function ( child ) {

            if ( child.isMesh ) {

                child.castShadow = true;
                child.receiveShadow = true;

            }

        } );

        console.info(object.toJSON());

        scene.add( object );

    } );
    }
   

    function animate() {
        requestAnimationFrame( animate );
        group.rotation.x = Math.PI/(i++);
        renderer.render( scene, camera );
        stats.update();
    }
}